<template>
  <div class="editor-container">
    <div class="question-tabs">
      <div v-for="(tab, index) in questionTypes" :key="index"
        :class="['tab-item', currentType === tab.value ? 'active' : '']" @click="changeQuestionType(tab.value)">
        {{ tab.label }}
      </div>
    </div>

    <div style="min-width: 100%;">
      <div v-if="currentType === 'multiple'">
        <AddMultipe></AddMultipe>
      </div>
      <div style="width: 100%;" v-if="currentType === 'single'">
        <AddSingle @emitData="handleChildData" :questionData="questionData"></AddSingle>

      </div>

      <div v-if="currentType === 'short'">
        <AddShort></AddShort>
      </div>
      <div v-if="currentType === 'judgment'">
        <AddJudgment @emitData="handleChildData"></AddJudgment>
      </div>
      <div v-if="currentType === 'video'">
        <AddVideo></AddVideo>
      </div>
      <div v-if="currentType === 'language'">
        <AddLanguage></AddLanguage>
      </div>
    </div>
    <Loading :size="60" color="var(--hover-color)" shadowColor="rgba(0, 0, 0, 0.5)" :speed="0.6" :loading="loading"
      backgroundColor="rgba(255, 255, 255, 0.8)">
    </Loading>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, nextTick ,defineEmits} from 'vue';
import AddJudgment from './AddJudgment.vue';
import AddMultipe from './AddMultipe.vue';
import AddSingle from './AddSingle.vue';
import AddShort from './AddShort.vue';
import AddVideo from './AddVideo.vue';
import AddLanguage from './AddLanguage.vue';
import { QuestionService } from '@/api/questionApi';
import Loading from '@/components/commont/Loading.vue';
const loading = ref(false)
const emit=defineEmits(['addShow'])
// 题型定义
const questionTypes = [
  { label: '单选题', value: 'single' },
  { label: '简答题', value: 'short' },
  { label: '判断题', value: 'judgment' },
  { label: '多选题', value: 'multiple' },
  { label: '语言题', value: 'language' },
  { label: '视频题', value: 'video' },

];

const currentType = ref('single');


// 编辑器引用
const titleEditor = ref(null);


// 问题数据
const questionData = reactive({
  title: '',
  difficulty: '0',
  options: [
    { content: '', checked: false }
  ],
  selectedOptionIndex: null, // 当前选中的选项索引
  judgmentAnswer: 'true',
  shortAnswer: '',
  languageType: 'chinese',
  timeLimit: 60
});

// 切换题型
const changeQuestionType = (type) => {
  currentType.value = type;

  // 根据题型初始化默认选项
  if (type === 'single' || type === 'multiple') {
    if (questionData.options.length === 0) {
      questionData.options = [{ content: '' }];
    }
  }
};

const handleChildData = async (data) => {
  console.log("wefghjkhgfd", data)

  try {
    loading.value = true;
  if (data.quType == 1) {
    if (!validateOptions(data.options)) {
      return;
    }
    if (data.content.trim() == '<p></p>') {
      alert('题目内容为空')
      return;
    }
  }
    const response = await QuestionService.addQuestion(data);
    if (response.code == 1) {
    loading.value = false;
    ElMessageBox.alert('题目添加成功', '', {
    confirmButtonText: '确定',
    callback: (action) => {
      if(action== 'confirm'||action== 'cancel'){
        emit('addShow',false)
      }
    },
  })
  }
  }
  finally {
    loading.value = false;
  }
};

const validateOptions = (options) => {
  // 1. 检查 options 是否为数组
  if (!Array.isArray(options)) {
    alert('options 不是数组')
    return false;
  }

  // 2. 检查数组长度是否为 2
  if (options.length <= 2) {
    alert('至少包含两个选项')
    return false;
  }

  // 3. 检查所有 content 是否为空字符串
  const hasEmptyContent = options.some(option => option.content.trim() === '');
  if (hasEmptyContent) {
    alert('选项包含为空的项')
    return false;
  }

  // 4. 检查是否有任意一个 checked 为 true
  const hasCheckedTrue = options.some(option => option.checked === true);
  if (!hasCheckedTrue) {
    alert('最少一个正确答案')
    return false;
  }
  // 如果所有条件都满足，返回 true
  console.log('options 验证通过');
  return true;
}


// 组件挂载后初始化
onMounted(() => {
  // 初始化编辑器
  if (titleEditor.value) {
    titleEditor.value.focus();
  }
});
</script>

<style scoped>
.editor-container {
  max-width: 100%;
  margin: 0 auto;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  background-color: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  font-family: Arial, sans-serif;
}

.question-tabs {
  display: flex;
  border-bottom: 1px solid #e0e0e0;
  overflow-x: auto;
  position: relative;
  /* 改善滚动条定位 */
}

/* WebKit浏览器滚动条样式 */
.question-tabs::-webkit-scrollbar {
  height: 8px;
  /* 水平滚动条高度 */
  background: transparent;
  /* 隐藏默认背景 */
}

.question-tabs::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
  margin: 2px 0;
  /* 添加边距使滚动条与内容保持距离 */
}

.question-tabs::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 4px;
  border: 1px solid #e0e0e0;
  /* 添加边框效果 */
  transition: background 0.3s ease;
}

.question-tabs::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* Firefox滚动条样式 */
.question-tabs {
  scrollbar-width: thin;
  scrollbar-color: #c1c1c1 #f1f1f1;
}

/* 滚动条悬停时显示阴影效果 */
.question-tabs:hover::-webkit-scrollbar-track {
  box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.1);
}

/* 添加滚动内容的过渡效果 */
.question-tabs>* {
  transition: transform 0.2s ease;
}

.tab-item {
  padding: 12px 24px;
  cursor: pointer;
  position: relative;
  color: #666;
  transition: color 0.3s;
  min-width: 90px;
  text-align: center;
}

.tab-item:hover {
  color: #4169e1;
}

.tab-item.active {
  color: #4169e1;
  font-weight: 500;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #4169e1;
}

.editor-body {
  padding: 20px;
  flex: 1;
}


.editor-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 16px 20px;
  border-top: 1px solid #e0e0e0;
}

.btn-cancel {
  padding: 8px 16px;
  border: 1px solid #dcdfe6;
  background-color: #fff;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
}

.btn-confirm {
  padding: 8px 16px;
  background-color: #4169e1;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
}

.btn-cancel:hover {
  border-color: #c6e2ff;
  color: #409eff;
}

.btn-confirm:hover {
  background-color: #365cc5;
}
</style>
